////////////////////////////////////////
//	File Header
//	Author: Stephen Ringer | stephen.ringer@gmail.com
//	File: SAGAD3DSkinManager.h
//
////////////////////////////////////////
#pragma once
////////////////////////////////////////
//	File Includes
#include <d3d9.h>
#include "../SAGARenderer/SAGARenderDevice.h"	// material manager base class
#include "SAGA.h"								// return values and structs
//
////////////////////////////////////////

////////////////////////////////////////
//	Macros
#define MAX_ID 65535
//
////////////////////////////////////////

////////////////////////////////////////
//	Class Name: SAGAD3DSkinManager
class SAGAD3DSkinManager : public SAGASkinManager 
{
	friend class SAGAD3DVCache;
	friend class SAGAD3DVCManager;
protected:
	LPDIRECT3DDEVICE9  m_pDevice;
	FILE              *m_pLog;

	HRESULT 		CreateTexture(SAGATEXTURE *pTexture, bool bAlpha);
	HRESULT			ConvertToNormalmap(SAGATEXTURE *pTexture);
	HRESULT 		SetAlphaKey(LPDIRECT3DTEXTURE9 *ppTexture, UCHAR R, UCHAR G, UCHAR B, UCHAR A);
	HRESULT 		SetTransparency(LPDIRECT3DTEXTURE9 *ppTexture, UCHAR Alpha);
	DWORD   		MakeD3DColor(UCHAR R, UCHAR G, UCHAR B, UCHAR A);
	void    		Log(char *, ...);
public:
	SAGAD3DSkinManager(LPDIRECT3DDEVICE9 pDevice, FILE *pLog);
	~SAGAD3DSkinManager();
	void			Reset();
	HRESULT 		AddSkin(const SAGACOLOR *pcAmbient, const SAGACOLOR *pcDiffuse,
							const SAGACOLOR *pcEmissive, const SAGACOLOR *pcSpecular,
							float fSpecPower, UINT *nSkinID);

	HRESULT 		AddTexture(	UINT nSkinID, const char *chName, bool bAlpha, float fAlpha, 
								SAGACOLOR *cColorKeys, DWORD dwNumColorKeys);
	HRESULT 		AddTextureHeightmapAsBump(UINT nSkinID, const char *chName);
	HRESULT			ExchangeTexture(UINT nSkinID, UINT nTexStage, const char *chName,
									bool bAlpha, float fAlpha, SAGACOLOR *cColorKeys, 
									DWORD dwNumColorKeys);

	HRESULT			ExchangeMaterial(	UINT nSkinID, const SAGACOLOR *pcAmbient, 
										const SAGACOLOR *pcDiffuse, const SAGACOLOR *pcEmissive, 
										const SAGACOLOR *pcSpecular, float fSpecPower);
	bool			MaterialEqual(const SAGAMATERIAL *pMat0, const SAGAMATERIAL *pMat1);
	inline bool		ColorEqual(const SAGACOLOR *pCol0, const SAGACOLOR *pCol1);
	UINT			GetNumSkins() { return m_nNumSkins; }
	SAGASKIN		GetSkin(UINT nSkinID);
	SAGAMATERIAL	GetMaterial(UINT nMatID);
	const char*		GetTextureName(UINT nID, float *pfAlpha, SAGACOLOR *pAK, UCHAR *pNum);
	void			LogCurrentStatus(char *chLog, bool bDetailed);
};
//
////////////////////////////////////////